- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 33.6k
url: improve performance of the format function #57099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
url: improve performance of the format function #57099
Conversation
| Review requested: 
 | 
64039ec    to
    bdd5eb4      
    Compare
  
            
          
                lib/url.js
              
                Outdated
          
        
      |  | ||
| search = search.replaceAll('#', '%23'); | ||
| // Escape '#' in search. | ||
| if (search.indexOf('#') !== -1) { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why didn't you use primordials here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm missing something but in this file the usage of primordials seems inconsistent now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't use primordials because I forgot one spot :) changing it now!
I had the same doubt as you, the file is using very few primordials. I will create another PR to convert the whole file to be using them as much as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah no problem I was just curious if there was a reason :)
        
          
                lib/url.js
              
                Outdated
          
        
      | search = search.replaceAll('#', '%23'); | ||
| // Escape '#' in search. | ||
| if (search.indexOf('#') !== -1) { | ||
| search = search.replace(/#/g, '%23'); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this got slower when # exists, but looks like an ok compromise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| search = search.replace(/#/g, '%23'); | |
| search = search.replaceAll('#', '%23'); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way this is because replaceAll has similar or better performance than a global static regexp, because it doesn't need to be compiled
To lower regexp usage I had made a PR replacing all these a while ago, so I think you can safely replace with replaceAll
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fascinating, it makes sense :) updated it with StringPrototypeReplaceAll
| Codecov ReportAll modified and coverable lines are covered by tests ✅ 
 Additional details and impacted files@@           Coverage Diff           @@
##             main   #57099   +/-   ##
=======================================
  Coverage   89.09%   89.09%           
=======================================
  Files         665      665           
  Lines      193249   193252    +3     
  Branches    37231    37222    -9     
=======================================
+ Hits       172175   172184    +9     
+ Misses      13802    13799    -3     
+ Partials     7272     7269    -3     
 🚀 New features to boost your workflow:
 | 
bdd5eb4    to
    247e1a2      
    Compare
  
    247e1a2    to
    1173315      
    Compare
  
    | @nodejs/performance can someone start a url benchmark ci? | 
| https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1657/ 
 | 
|  | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
| antything else needed here? | 
| can I please ask for a rerun of the CI? | 
| Landed in 723d7bb | 
PR-URL: #57099 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #57099 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
As part of my ongoing review of the
assertcodebase, I started examining thelib/url.jsfile and optimizing some straightforward code that could be further refined for better performance.Here are the benchmark results for the
benchmarks/url/url-format.jsfile:For type="slashes":
123% improvement
For type="file":
50% improvement